﻿/*
 * Author: Roy Scheefhals
 * 
 * Description: Edge detection filter to subtract VERTICAL edges
 * */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LibraryCS
{
    public class SobelEdgeVertical : VisionAlgorithm
    {

        //mask used for detection
        int[,] sobelX_mask = { { -1, 0, +1 },
                               { -2, 0, +2 },
                               { -1, 0, +1 } };

        public SobelEdgeVertical(String name) : base(name){
            
        }

        public override Bitmap DoAlgorithm(Bitmap sourceImage)
        {
            VImage mi = new VImage(sourceImage);

            byte[] outputimage = new byte[mi.Width * mi.Height];
            byte[] grayvalues = mi.GrayPixels;
            int y = 1;//y
            int x = 1;//x
            int Gx = 0;
            //process whole image
            for (; y < mi.Height - 1; y++)
            {
                for (x = 1; x < mi.Width - 1; x++)
                {
                    Gx = 0; 
                    // pass through the kernel to get the value
                    // and apply the convolution mask
                    for (int ykernel = -1; ykernel <= 1; ykernel++)//rows
                    {
                        for (int xkernel = -1; xkernel <= 1; xkernel++)//cols
                        {
                            
                            int grayval = grayvalues[((y + ykernel) * mi.Width) + (xkernel + x)];
                            Gx += sobelX_mask[ykernel + 1, xkernel + 1] * grayval;
                        }
                    }

                    //get magnitude and save in output array
                    int magnitude = Math.Abs(Gx);
                    if (magnitude > 255)
                        magnitude = 255;
                    outputimage[(y * mi.Width) + x] = (byte)magnitude;

                }
            }

            Bitmap output = mi.returnNewBitmapOutput(outputimage);
            return output;
        }
    }
}
